<template>
  <el-dialog
    title="开始审批"
    :visible="visible"
    width="700px"
    :before-close="closeForm"
    :close-on-click-modal="false"
  >
    <el-tabs v-model="active">
      <el-tab-pane label="基础信息" name="basic">
        <el-form label-width="200px">
          <el-form-item label="节点编号">
            {{ nodeCode }}
          </el-form-item>
          <el-form label-width="200px">
            <el-form-item label="是否允许撤回">
              <el-switch
                v-model="isAllowCancel"
                :disabled="readonly"
              />
            </el-form-item>
            <el-form-item label="是否上传附件">
              <el-switch
                v-model="isUpFile"
                :disabled="readonly"
              />
            </el-form-item>
            <el-form-item label="是否对文件签名">
              <el-switch
                v-model="isUpFileSign"
                :disabled="readonly"
              />
            </el-form-item>
            <el-form-item label="审核限制时间" prop="LimitTime">
              <el-input-number v-model="limitTime" :disabled="readonly" placeholder="审核限制时间" clearable />
              <span style="padding-left: 10px;">分钟</span>
            </el-form-item>
            <el-form-item label="存在多个可入节点时">
              <enumItem
                v-model="outPolicy"
                :readonly="readonly"
                :dic-key="EnumDic.FlowNodeOutPolicy"
                placeholder="存在多个可入节点时处理方式"
                sys-head="flow"
              />
            </el-form-item>
          </el-form>
          <div v-if="readonly == false" style="text-align: center">
            <el-button type="primary" @click="save">保存</el-button>
            <el-button @click="resetForm">重置</el-button>
          </div>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="表单配置" name="formEdit">
        <nodeFormEdit :readonly="readonly" :def-form-id="defFormId" :source="source" :flow-id="flowId" :node-id="nodeId" :is-load="active=='formEdit'" @refresh="saveForm" />
      </el-tab-pane>
      <el-tab-pane label="表单权限" name="formPower">
        <nodeFormPower :readonly="readonly" :flow-id="flowId" :form-id="curFormId" :is-allow-edit="source.IsAllowEdit" :node-id="nodeId" :is-load="active=='formPower'" />
      </el-tab-pane>
      <el-tab-pane label="审批事件" name="nodeEv">
        <nodeEventEdit :filter-event="(val) => val <= 1" :readonly="readonly" :flow-id="flowId" :node-id="nodeId" :node-event.sync="source.NodeEvent" :is-load="active=='nodeEv'" />
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import { DictItemDic, FlowEventList, EnumDic } from '@/flow/config/flowConfig'
import * as flowNodeApi from '@/flow/api/flowNode'
import nodeFormEdit from './nodeFormEdit.vue'
import nodeFormPower from './nodeFormPower.vue'
import nodeEventEdit from './nodeEventEdit.vue'
export default {
  components: {
    nodeFormEdit,
    nodeFormPower,
    nodeEventEdit
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    nodeId: {
      type: String,
      default: null
    },
    flowId: {
      type: String,
      default: null
    },
    defFormId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      EnumDic,
      FlowEventList,
      DictItemDic,
      outPolicy: null,
      limitTime: null,
      nodeCode: null,
      isUpFile: false,
      isUpFileSign: false,
      active: 'basic',
      curFormId: null,
      source: {},
      isAllowCancel: false,
      isAllowEdit: true
    }
  },
  computed: {
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    tabClick(key) {
      this.active = key
    },
    saveForm(data) {
      this.source.FormId = data.FormId == null ? null : data.FormId
      this.source.IsAllowEdit = data.IsAllowEdit
      this.curFormId = data.FormId == null ? this.defFormId : data.FormId
    },
    async save() {
      await flowNodeApi.SetBeginNode(this.nodeId, {
        IsAllowCancel: this.isAllowCancel,
        LimitTime: this.limitTime,
        OutPolicy: this.outPolicy,
        IsUpFile: this.isUpFile,
        IsUpFileSign: this.isUpFileSign
      })
      this.$message({
        message: '更新成功!',
        type: 'success'
      })
    },
    async reset() {
      const data = await flowNodeApi.Get(this.nodeId)
      this.source = data
      this.isAllowCancel = data.IsAllowCancel
      this.limitTime = data.LimitTime
      this.outPolicy = data.OutPolicy
      this.isUpFile = data.IsUpFile
      this.isUpFileSign = data.IsUpFileSign
      this.nodeCode = data.NodeCode
      this.curFormId = data.FormId == null ? this.defFormId : data.FormId
    },
    closeForm() {
      this.$emit('update:visible', false)
      this.$emit('close', false)
    },
    resetForm() {
      this.isUpFile = this.source.IsUpFile
      this.isUpFileSign = this.source.IsUpFileSign
      this.isAllowCancel = this.source.IsAllowCancel
      this.outPolicy = this.source.OutPolicy
      this.curFormId = this.source.FormId == null ? this.defFormId : this.source.FormId
      this.limitTime = this.source.LimitTime
    }
  }
}
</script>
